package cn.dnui_yxk801.dnuifood_16110100801.controller;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import java.util.List;

import cn.dnui_yxk801.dnuifood_16110100801.Adapter.FoodAdapter;
import cn.dnui_yxk801.dnuifood_16110100801.Adapter.FoodListAdapter;
import cn.dnui_yxk801.dnuifood_16110100801.R;
import cn.dnui_yxk801.dnuifood_16110100801.bean.FoodBean;
import cn.dnui_yxk801.dnuifood_16110100801.listener.OnRetrofitListener;
import cn.dnui_yxk801.dnuifood_16110100801.model.FoodModel;
import cn.dnui_yxk801.dnuifood_16110100801.model.ShopModel;

public class SearchResultActivity extends BaseActivity implements OnRetrofitListener{
    private RecyclerView rv_search;
    private String search;

    @Override
    void initViews() {
        layout_file=R.layout.activity_search_result;
        setLayout(layout_file);
        rv_search=findViewById(R.id.rv_search);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        // 设置布局管理器
        rv_search.setLayoutManager(layoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        rv_search.setHasFixedSize(true);
    }

    @Override
    void initEvents() {

    }

    @Override
    void initDatas() {
        FoodModel foodModel=new FoodModel();
        foodModel.getFoodBySearch(search,this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent=getIntent();
        search=intent.getStringExtra("search");
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onSuccess(Object object, int flag) {
        if (flag==8){
            List<FoodBean> list=(List<FoodBean>)object;
            FoodListAdapter foodListAdapter=new FoodListAdapter(this,list);
            rv_search.setAdapter(foodListAdapter);
            if (list.size()==0)
                Toast.makeText(this,"没有搜索结果",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFalure(String msg) {

    }
}
